from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.status import HTTP_201_CREATED,HTTP_404_NOT_FOUND,HTTP_204_NO_CONTENT
from study.models import Student, Classes
from study.serializers import StudentSerializer, ClassesSerializer

# Create your views here.

"""
class StudenViewSet(viewsets.ViewSet):
    def list(self,request):
        #获取queryset
        queryset=Student.objects.all()
        #序列化 实例化序列化器  将查询集转为了json格式的数据
        serializer=StudentSerializer(queryset,many=True)
        return Response(serializer.data)

    def create(self,request):
        '''
        1.获取前端传入的数据
        2.校验
        3.保存数据
        4,返回响应
        :param request:
        :return:
        '''
        #获取数据
        data=request.data
        #反序列化  要转成模型对象
        serializer=StudentSerializer(data=data)

        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data,status=HTTP_201_CREATED)

    def retrieve(self,request,pk):
        '''
        1.依据pk值查询指定对象
            如果有，序列化
            如果没有，返回404，找不到学生信息
        2.返回响应
        :param request:
        :param pk:
        :return:
        '''
        try:
            student=Student.objects.get(id=pk)
        except Student.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)

        serializer=StudentSerializer(instance=student)
        return Response(serializer.data)

    def update(self,request,pk):
        '''
         1.依据pk值查询指定对象
            如果有，序列化
            如果没有，返回404，找不到学生信息
         2.获取前端传入的数据
         3.校验
         4.保存数据
         5.返回响应
        :param request:
        :param pk:
        :return:
        '''
        try:
            student=Student.objects.get(id=pk)
        except Student.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)

        serializer=StudentSerializer(instance=student,data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def destroy(self,request,pk):
        try:
            student=Student.objects.get(id=pk)
        except Student.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)

        student.delete()  #数据会被直接删除
        return Response(status=HTTP_204_NO_CONTENT)

"""

class StudentViewSet(viewsets.ModelViewSet):
    # queryset = Student.objects.all()
    queryset = Student.objects.filter(is_delete=False)
    serializer_class = StudentSerializer


class ClassesViewSet(viewsets.ModelViewSet):
    '''
    list:
    查看全部班级信息
    create:
    新增班级信息
    retrieve:
    查看某个班级信息
    update:
    修改某个班级信息
    destroy:
    删除某个班级信息
    last:
    查看最新班级信息
    '''
    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassesSerializer

    @action(methods=['get'],detail=False)   #detail=  如果等于True，则要带pk
    def last(self,request):
        last_class=Classes.objects.filter(is_delete=False).last()
        serializer=self.get_serializer(last_class)      #序列化
        return Response(serializer.data)